package com.gg.helper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
* 一句话连接数据库
* */
public class DBHelper {
    private static  Connection  con =  null ;

    /*
    * 1.准备4大参数
    * */
    private  static final String  user =  "root";
    private  static final String  password =  "123456";//注意：改成你自己的密码！！！
    private  static  final String  url =
            "jdbc:mysql:///fruit?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8";
    //private  static final  String  className =  "com.mysql.jdbc.Driver";//5
    private  static final  String  className =  "com.mysql.cj.jdbc.Driver";//8
    /*
    * 2.加载驱动
    * */
    /*静态块会被自动在加载执行，当且仅当被加载执行一次*/
    static {
        try {
            //try里放的是有可能出现异常的代码
            Class.forName(className);

        } catch (ClassNotFoundException e) {
            //catch里放的是出现异常你要干啥
            //向控制台输出错误信息
            throw new RuntimeException(e);
        }
    }


    /*
    * 3.获取连接
    * */
    public  static Connection  getConnection(){
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return  con ;
    }




}
